﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using SaaS;
public partial class AddItems : System.Web.UI.Page
{
    XmlDocument xmldoc = new XmlDocument();
    XmlDocument xmldata = new XmlDocument();
    //set table's ID if this is an edit or data is being entered
    int tableID = -1;
    //table name
    String tableName;
    //table descrition
    String tableDescription;
    //data comment
    String dataComment;
    protected void Page_Load(object sender, EventArgs e)
    {
        string[] typeString = { "int", "double", "bool", "string" };
        if (DropDownList_type.Items.Count == 0)
            for (int i = 0; i < typeString.Length; i++)
            {
                DropDownList_type.Items.Add(typeString[i]);
            } 

        lblErr.Text = "";
        try
        {
            xmldoc.Load(Server.MapPath(@"App_Data\temp.xml"));
            if (this.TB_Items.Text.Trim() != xmldoc.DocumentElement.Name)
            {
                xmldoc = new XmlDocument();
                string temp = "<" + this.TB_Items.Text.Trim() + "><" + "/"
                    + this.TB_Items.Text.Trim() + ">";
                if (this.TB_Items.Text.Trim() == "")
                {
                    temp = "<columns></columns>";
                }

                xmldoc.LoadXml(temp);
                xmldoc.Save(Server.MapPath(@"App_Data\temp.xml"));
            }
        }
        catch (System.Exception ex)
        {
            string temp = "<" + this.TB_Items.Text.Trim() + "><" + "/"
                + this.TB_Items.Text.Trim() + ">";
            if (this.TB_Items.Text.Trim() == "")
            {
                temp = "<columns></columns>";
            }
            xmldoc.LoadXml(temp);
            xmldoc.Save(Server.MapPath(@"App_Data\temp.xml"));
        }
    }

    protected void ADD_click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        if (TB_Atr.Text.Length <= 0)
        {
            lblErr.Text = "Please provide an attribute name";
            return;
        }
        xmldoc.Load(Server.MapPath(@"App_Data\temp.xml"));
        int count = xmldoc.DocumentElement.ChildNodes.Count + 1;

        SaaS.AddItemService addService = new SaaS.AddItemService();
        xmldoc = addService.addAttribute(xmldoc, TB_Atr.Text.Trim(), DropDownList_type.SelectedValue,
            this.rdoNullTrue.Checked, this.TB_DF.Text.Trim(), count, this.rdoPriKeyTrue.Checked);

        xmldoc.Save(Server.MapPath(@"App_Data\temp.xml"));

        TB_Atr.Text = "";
        rdoNullTrue.Checked = true;
        rdoPriKeyFalse.Checked = true;
        TB_DF.Text = "";
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(Server.MapPath("~/App_Data/temp.xml"));
        this.GridView1.DataMember = "col";
        this.GridView1.DataSource = dataSet;
        this.GridView1.DataBind();
    }

    protected void FINISH_click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        if (TB_Items.Text.Length <= 0)
        {
            lblErr.Text = "Please provide a name for the object";
            return;
        }
        else if (xmldoc.DocumentElement.ChildNodes.Count <= 0)
        {
            lblErr.Text = "Please add at least 1 attribute to the table";
            return;
        }
        xmldoc.Load(Server.MapPath(@"App_Data\temp.xml"));
        tableDescription = this.TB_DCP.Text;

        SaaS.AddItemService addService = new SaaS.AddItemService();
        string errCode = addService.finish(xmldoc, tableDescription, Session["LoginID"].ToString(), -1, this.TB_Items.Text);
        
        if (errCode.Length > 0)
            lblErr.Text = errCode;
        else
        {
            TB_Items.Text = "";
            TB_DCP.Text = "";
        }
    }
    protected void DropDownList_type_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}